package com.base.myproject.client.HR.bcode;

import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.BaseOpenJSP;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Label;

/**
 * @author cxghiu410@gmail.com
 *	人事报表
 */
public class Rpt_PersonnelStatements extends BaseJDBCPanel {
	DateField start = new DateField();
	DateField end = new DateField();
	Button search = new Button("查询");
	private Button toExcel = new Button("导出到EXCEL");
	String jspFile = "hr_Jsp/rptPersonnelStatements.jsp";
	public Rpt_PersonnelStatements() {
		init();
	}
	
	public Rpt_PersonnelStatements(String jspFile) {
		this.jspFile = jspFile;
		init();
	}
	
	public void complete() {
		super.complete();
		print.addListener(Events.OnClick, getListener());
		getToolbarll().add(print);
		getToolbarll().add(toExcel);
	}
	
	BaseOpenJSP jsp;
	protected void init() {
		start.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		end.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		
		search.addListener(Events.OnClick, getListener());
		VerticalPanel vp = new VerticalPanel();
		HorizontalPanel hp = new HorizontalPanel();
		hp.setSpacing(10);
		hp.add(new Label("查询日期从："));
		hp.add(start);
		hp.add(new Label("到："));
		hp.add(end);
		hp.add(search);
		
		vp.add(hp);
		jsp = new BaseOpenJSP(jspFile);
		Tools.setResize(jsp, 0.99, 0.89);
		vp.add(jsp);
		add(vp);
	}

	Listener<BaseEvent> listener;
	private Listener<? extends BaseEvent> getListener() {
		if (listener != null) 
			return listener;
		listener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				if (be.getSource().equals(search)) {
					String startDate = start.getRawValue();
					String endDate = end.getRawValue();
					if (startDate.equals("") || endDate.equals("")) {
						MessageBox.alert("提示", "请选择查询时间段，谢谢", null);
						return;
					}
					
					if (start.getValue().getTime() > end.getValue().getTime()) {
						MessageBox.alert("提示", "结束日期必须大于等于开始日期，请重新填写时间段，谢谢", new Listener<MessageBoxEvent>() {

							@Override
							public void handleEvent(MessageBoxEvent be) {
								end.setValue(null);
								end.focus();
							}
							
						});
						return;
					}
					jsp.getFrame().setUrl(jspFile + "?start=" + startDate + "&end=" + endDate);
				} else {
					jsp.Print();
				}
			}
		};
		return listener;
	}
}
